package cn.strategy.fund;

import cn.math.MyStatistics;
import cn.model.Mmaidian;
import cn.model.Mmstock;
import org.junit.Test;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 最近高点到低点有 20% 跌幅的
 * */
public class Mai_dian_case2 {

    @Test
    public void test_1() {
        List<Mmaidian> lst = new ArrayList<>();
        lst.add(new Mmaidian(1D, false, null, 0));
        lst.add(new Mmaidian(2D, true, null, 0));
        lst.add(new Mmaidian(3D, false, null, 0));

        Mmaidian f = first(lst);
        System.out.println(f.toString());

        List<Mmaidian> lst1 = rest(lst);
        for (Mmaidian m: lst1) {
            System.out.println(m.toString());
        }
    }

    @Test
    public void show_mai_dian() throws ClassNotFoundException, SQLException {
        Class.forName("org.h2.Driver");
        Connection conn = DriverManager.getConnection("jdbc:h2:~/data;AUTO_SERVER=TRUE", "root", "root");

        String sql = "SELECT symbol FROM type_stock GROUP BY symbol";
        PreparedStatement stmt = conn.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            //getData_ex(conn, rs.getString(1));
            getHL(conn, rs.getString(1));
        }

        if (rs != null) rs.close();
        if (stmt != null) stmt.close();
        if (conn != null) conn.close();
    }

    public Mmaidian first(List<Mmaidian> lst) {
        if (lst != null && lst.size() > 0) {
            return lst.get(0);
        }
        return null;
    }

    public List<Mmaidian> rest(List<Mmaidian> lst) {
        if (lst != null && lst.size() > 0) {
            return lst.subList(1, lst.size());
        }
        return null;
    }

    public void getHL(Connection conn, String symbol) {
        String sql = "SELECT price, h, STOCK_DATE FROM mai_dian WHERE SYMBOL = '"+ symbol +"' and h = 0 ORDER BY STOCK_DATE DESC";

        PreparedStatement stmt = null;
        ResultSet rs = null;

        List<Mmaidian> lst = new ArrayList<>();
        int count = 0;

        try {
            stmt = conn.prepareStatement(sql);

            // 5. 执行查询并获取结果集
            rs = stmt.executeQuery();

            // 6. 遍历结果集并处理数据
            while (rs.next()) {
                if (count <= 10) {
                    count++;
                } else {
                    break;
                }

                lst.add(new Mmaidian(rs.getDouble(1), rs.getBoolean(2), rs.getDate(3), 0));
            }

            getHL1(conn, symbol, lst);

        } catch (SQLException e) {
            System.err.println("数据库操作失败！错误信息：" + e.getMessage());
            e.printStackTrace();
        } finally {
            // 7. 关闭资源（反向顺序）
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void getHL1(Connection conn, String symbol, List<Mmaidian> lst) {
        Mmaidian m = first(lst);
        if (m != null) {
            List<Mmaidian> sub = rest(lst);
            if (sub != null) {
                Mmaidian m1 = first(sub);
                if (m1 != null) {
                    if (m.getH() == false && m1.getH() == false) {
                        getHL1(conn, symbol, sub);
                    } else if (m.getH() == false && m1.getH() == true) {
                        Double v = m1.getPrice() / m.getPrice();
                        Mmstock p = getLastPrice(conn, symbol);
                        Double pv = p.getPrice() / m.getPrice();
                        if (v > 1.05D && v < 1.25D && pv > 0.95D && pv < 1.05D) {
                            System.out.println(symbol + " " + p.getName());
                        }
                    }
                }
            }
        }
    }

    public Mmstock getLastPrice(Connection conn, String symbol) {
        String sql = "SELECT PRICE, NAME FROM df_stock_date WHERE SYMBOL = '"+ symbol +"' ORDER BY STOCK_DATE DESC LIMIT 0, 1";

        PreparedStatement stmt = null;
        ResultSet rs = null;
        Mmstock last_price = new Mmstock();

        try {
            stmt = conn.prepareStatement(sql);

            // 5. 执行查询并获取结果集
            rs = stmt.executeQuery();

            // 6. 遍历结果集并处理数据
            while (rs.next()) {
                last_price.setPrice(rs.getDouble(1));
                last_price.setName(rs.getString(2));
            }

        } catch (SQLException e) {
            System.err.println("数据库操作失败！错误信息：" + e.getMessage());
            e.printStackTrace();
        } finally {
            // 7. 关闭资源（反向顺序）
            try {
                if (rs != null) rs.close();
                if (stmt != null) stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return last_price;
    }
}













































































